/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import model.Result;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.function.PolynomialFunction2D;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.data.statistics.Regression;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import java.awt.Dimension;



/**
 *
 * @author BENAMMAR Riyadh
 */
public class GraphicView {

    public GraphicView(ArrayList<Result> results) {
        
        ChartPanel chartP1 = CreateChart1(results);
        ChartPanel chartP2 = CreateChart2(results);
        ChartPanel chartP3 = CreateChart3(results);
        ChartPanel chartP4 = CreateChart4(results);
        JPanel tableP1 = CreateTable1(results);
        
        JFrame frame=new JFrame();
        JTabbedPane  tabbedPane= new JTabbedPane();
        JPanel panel= new JPanel();
        tabbedPane.addTab("Prblm Len vs Duration", chartP1);
        tabbedPane.addTab("Prblm Len vs Solut. Len", chartP2);
        tabbedPane.addTab("Prblm Len vs Space", chartP3);
        tabbedPane.addTab("Prblm Len vs Efficienty", chartP4);
        tabbedPane.addTab("Result", tableP1);
        panel.setBounds(0, 0, Toolkit.getDefaultToolkit().getScreenSize().height, Toolkit.getDefaultToolkit().getScreenSize().width);
        panel.repaint();
        frame.getContentPane().add(tabbedPane);
        frame.getContentPane().add(panel, BorderLayout.SOUTH);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.getContentPane().setLayout(new BorderLayout());
        frame.setVisible(true);
    }
    
    private JPanel CreateTable1(ArrayList<Result> results) {
		// TODO Auto-generated method stub
    	DefaultTableModel model = new DefaultTableModel();
    	model.addColumn("Algorithm");
    	model.addColumn("A");
    	model.addColumn("B");
    	model.addColumn("Solution");
    	model.addColumn("Problem lenght");
    	model.addColumn("Solution lenght");
    	model.addColumn("Duration");
    	model.addColumn("Max used memory");
    	for(Result r:results){
    		String[] line = {r.getAlgorithm(),r.getA(),r.getB(),r.getSolution(),String.valueOf(r.getProblemLength()),String.valueOf(r.getSolutionLength()),String.valueOf(r.getDuration()),String.valueOf(r.getSpace())};
    		model.addRow(line);
    	}
    	JTable t = new JTable(model);
    	JScrollPane sp = new JScrollPane(t);
    	JPanel p = new JPanel();
    	p.add(sp);
		return p;
	}

	public ChartPanel CreateChart1(ArrayList<Result> results){
    	HashMap<String,XYSeries> xyseries = new HashMap<String, XYSeries>();
        
    	double maxL=0;
    	
        String algo;
        for(Result r:results){
        	algo=r.getAlgorithm();
        	if (!xyseries.containsKey(algo)){
        		xyseries.put(algo, new XYSeries(algo));
        	}
        	maxL= Math.max(maxL, r.getProblemLength());
            xyseries.get(algo).add(r.getProblemLength(),r.getDuration());
        }
        
        

        XYSeriesCollection seriesCollection=new XYSeriesCollection();

        for(java.util.Map.Entry<String, XYSeries> s:xyseries.entrySet()){
        	seriesCollection.addSeries(s.getValue());
        }
        
        JFreeChart chart1= ChartFactory.createXYLineChart("Performance Analyser", "String length", "Execution time in ns", seriesCollection, PlotOrientation.VERTICAL, true, true, false);        //chart.setBackgroundPaint(Color.GREEN);
        ChartPanel chartP1 =new ChartPanel(chart1);
        XYPlot plot=chart1.getXYPlot();
        if(xyseries.containsKey("Dynamic") && xyseries.get("Dynamic").getItemCount()>3 && false){
        double[] ad = Regression.getPolynomialRegression(new XYSeriesCollection(xyseries.get("Dynamic")), 0,2);
        PolynomialFunction2D polynomialfunction2d = new PolynomialFunction2D(ad);
        XYDataset xydataset = DatasetUtilities.sampleFunction2D(polynomialfunction2d, 0D,maxL , 100, "y="+((int)ad[0])+"+"+(ad[1])+"x+"+((int)ad[2])+"x^2");
        plot.setDataset(1, xydataset);
        }
        plot.setOutlinePaint(Color.blue);
        return chartP1;
        
        
    }
    
    public ChartPanel CreateChart2(ArrayList<Result> results){
    	HashMap<String,XYSeries> xyseries = new HashMap<String, XYSeries>();
    	        
    	        String algo;
    	        
    	        for(Result r:results){
    	        	algo=r.getAlgorithm();
    	        	if (!xyseries.containsKey(algo)){
    	        		xyseries.put(algo, new XYSeries(algo));
    	        	}
    	            xyseries.get(algo).add(r.getProblemLength(),r.getSolutionLength());            
    	        }
    	        
    	        XYSeriesCollection seriesCollection=new XYSeriesCollection();
    	        for(java.util.Map.Entry<String, XYSeries> s:xyseries.entrySet()){
    	        	seriesCollection.addSeries(s.getValue());
    	        }
    	        
    	        JFreeChart chart1= ChartFactory.createXYLineChart("Performance Analyser", "String length", "Solution lenght", seriesCollection, PlotOrientation.VERTICAL, true, true, false);        //chart.setBackgroundPaint(Color.GREEN);
    	        ChartPanel chartP1 =new ChartPanel(chart1);
    	        XYPlot plot=chart1.getXYPlot();
    	        plot.setOutlinePaint(Color.blue);
    	        return chartP1;
    	        
    	        
    	    }
    public ChartPanel CreateChart3(ArrayList<Result> results){
    	HashMap<String,XYSeries> xyseries = new HashMap<String, XYSeries>();
    	        
    	        String algo;
    	        
    	        for(Result r:results){
    	        	algo=r.getAlgorithm();
    	        	if (!xyseries.containsKey(algo)){
    	        		xyseries.put(algo, new XYSeries(algo));
    	        	}
    	            xyseries.get(algo).add(r.getProblemLength(),r.getSpace());            
    	        }
    	        
    	        XYSeriesCollection seriesCollection=new XYSeriesCollection();
    	        for(java.util.Map.Entry<String, XYSeries> s:xyseries.entrySet()){
    	        	seriesCollection.addSeries(s.getValue());
    	        }
    	        
    	        JFreeChart chart1= ChartFactory.createXYLineChart("Performance Analyser", "String length", "Max space used", seriesCollection, PlotOrientation.VERTICAL, true, true, false);        //chart.setBackgroundPaint(Color.GREEN);
    	        ChartPanel chartP1 =new ChartPanel(chart1);
    	        XYPlot plot=chart1.getXYPlot();
    	        plot.setOutlinePaint(Color.blue);
    	        return chartP1;
    	        
    	        
    	    }
    public ChartPanel CreateChart4(ArrayList<Result> results){
    	HashMap<String,XYSeries> xyseries = new HashMap<String, XYSeries>();
    	        
    	        String algo;
    	        
    	        for(Result r:results){
    	        	algo=r.getAlgorithm();
    	        	if (!xyseries.containsKey(algo)){
    	        		xyseries.put(algo, new XYSeries(algo));
    	        	}
    	            xyseries.get(algo).add(r.getProblemLength(),(r.getDuration()/(r.getSolutionLength()+1)));            
    	        }
    	        
    	        XYSeriesCollection seriesCollection=new XYSeriesCollection();
    	        for(java.util.Map.Entry<String, XYSeries> s:xyseries.entrySet()){
    	        	seriesCollection.addSeries(s.getValue());
    	        }
    	        
    	        JFreeChart chart1= ChartFactory.createXYLineChart("Performance Analyser", "String length", "Time spend/Solution lenght", seriesCollection, PlotOrientation.VERTICAL, true, true, false);        //chart.setBackgroundPaint(Color.GREEN);
    	        ChartPanel chartP1 =new ChartPanel(chart1);
    	        XYPlot plot=chart1.getXYPlot();
    	        plot.setOutlinePaint(Color.blue);
    	        return chartP1;
    	        
    	        
    	    }
    
    
}
